var api = require('../../config/api.js');
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    inputShowed: false,
    inputVal: "",

    myAgentList: {
      hasMore: true,
      list: [],
      listSize: 0
    },
    startNum: 1, //页数

    myAgentList1: {
      hasMore: true,
      list: [],
      listSize: 0
    },
    startNum1: 1,

    tabs: ["一级", "二级", "三级"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,

    tabs1: ["已审核", "待审核"],
    activeIndex1: 0,
    sliderOffset1: 50,
    sliderLeft1: 0,

    num: ['0', '0', '0'],
    num1: [0, 0],

    search: false,

    texhid: 0,
    texhid1: 0,

    usertype: "",

    apply0: 0,
    apply1: 1,

  },

  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false,
      search: false,
      texhid: 0,
      texhid1: 0,
    });
    this.onLoad();
    // this.onShow();
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },

  searInput: function (e) {
    var _that = this
    var all = 0
    console.log(_that.data.inputVal)
    wx.showLoading({
      title: '搜索中',
    })

    if (_that.data.usertype == "2") {
      var _url1 = api.SalerISList + '?current=1&size=10';
    } else if (_that.data.usertype == "3") {
      var _url1 = api.SalerOnList + '?current=1&size=10';
    }

    if (_that.data.inputVal.length != '') {
      wx.request({
        url: _url1,
        data: {
          deptName: _that.data.inputVal,
        },
        method: 'POST',
        header: {
          'Accept': 'application/json',
          'ACCESS-TOKEN': wx.getStorageSync('token')
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          if (res.data.code == '1') {
            var listSize = res.data.obj.records.length;
            console.log('listSize=' + listSize);
            var newData = _that.data.myAgentList.list;
            newData = res.data.obj.records;
            var num1 = res.data.obj.total;
            all = all + num1
            console.log("all=" + all)
            var newList = {};
            //当长度小于10表示没有下页数据了
            if (listSize < 10) {
              newList.hasMore = false;
            } else {
              newList.hasMore = true;
            }

            if (listSize == 0) {
              _that.setData({
                texhid: 1,
              })
              console.log('texhid=' + _that.data.texhid)
            } else {
              _that.setData({
                texhid: 0,
              })

            }
            newList.list = newData;
            console.log('newData=' + newData);
            newList.listSize = listSize;
            _that.setData({
              myAgentList: newList
            });

            console.log('myAgentList=' + _that.data.myAgentList);
            _that.data.num1[0] = num1
            _that.setData({
              startNum: 2,
              "num1[0]": num1
            })
          } else {
            // util.msg("错误", res.data.msg)
            console.log("网络通，数据不成功")
          }

          wx.request({
            url: api.SalerCheckList + '?current=1&size=10',
            data: {
              deptName: _that.data.inputVal,
            },
            method: 'POST',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },
            success: function (res) {
              console.log(res.data)
              wx.hideLoading();
              if (res.data.code == '1') {
                var listSize = res.data.obj.records.length;
                console.log('listSize=' + listSize);
                var num2 = res.data.obj.total;
                all = all + num2
                console.log("all=" + all)
                _that.setData({
                  "num[0]": all,
                  search: true
                })
                var newData = _that.data.myAgentList1.list;
                newData = res.data.obj.records;
                var newList = {};
                //当长度小于10表示没有下页数据了
                if (listSize < 10) {
                  newList.hasMore = false;
                } else {
                  newList.hasMore = true;
                }

                if (listSize == 0) {
                  _that.setData({
                    texhid1: 1,
                  })
                } else {
                  _that.setData({
                    texhid1: 0,
                  })
                }
                newList.list = newData;
                console.log('newData=' + newData);
                newList.listSize = listSize;
                _that.setData({
                  myAgentList1: newList,
                  "num1[1]": num2
                });

                console.log('myAgentList1=' + _that.data.myAgentList1);

                _that.setData({
                  startNum1: 2
                })
              } else {
                // util.msg("错误", res.data.msg)
                console.log("网络通，数据不成功")
              }
            },
            fail: function (res) {
              wx.hideLoading();
              console.log(res.data)
              // util.msg("错误", "通讯失败")
            }
          })


        },
        fail: function (res) {
          wx.hideLoading();
          console.log(res.data)
          // util.msg("错误", "通讯失败")
        }
      })

    } else {
      wx.showToast({
        title: "输入内容不能为空",
        icon: 'none',
        duration: 2000
      });
    }



  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //加载页面
    var _that = this
    var all = 0
    wx.getSystemInfo({
      success: function (res) {
        _that.setData({
          sliderLeft: (res.windowWidth / _that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / _that.data.tabs.length * _that.data.activeIndex
        });

        _that.setData({
          sliderLeft1: (res.windowWidth / _that.data.tabs1.length - sliderWidth) / 2,
          sliderOffset1: res.windowWidth / _that.data.tabs1.length * _that.data.activeIndex1
        });
      }
    });

    _that.setData({
      usertype: wx.getStorageSync('usertype')
    })


    console.log('usertype=' + _that.data.usertype)
    if (_that.data.usertype == "2") {
      var _url1 = api.SalerISList + '?current=1&size=5';
      var _url2 = api.SalerCheckList + '?current=1&size=5';
    } else if (_that.data.usertype == "3") {
      var _url1 = api.SalerOnList + '?current=1&size=5';
      var _url2 = api.SalerCheckList + '?current=1&size=5';
    }

    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: _url1,
      data: {
        current: "1", //请求第一页
        size: "5"
      },
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        if (res.data.code == '1') {
          var listSize = res.data.obj.records.length;
          console.log('listSize=' + listSize);
          var newData = _that.data.myAgentList.list;
          newData = res.data.obj.records;
          var num1 = res.data.obj.total;
          all = all + num1
          console.log("all=" + all)
          var newList = {};
          //当长度小于10表示没有下页数据了
          if (listSize < 5) {
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          console.log('newData=' + newData);
          newList.listSize = listSize;
          _that.setData({
            myAgentList: newList,
            "num1[0]": num1
          });

          console.log('myAgentList=' + _that.data.myAgentList);

          _that.setData({
            startNum: 2
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络通，数据不成功")
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.data)
        // util.msg("错误", "通讯失败")
      }
    })

    wx.request({
      url: _url2,
      data: {
        current: "1", //请求第一页
        size: "5"
      },
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        if (res.data.code == '1') {
          var listSize = res.data.obj.records.length;
          console.log('listSize=' + listSize);
          var num2 = res.data.obj.total;
          all = all + num2
          console.log("all=" + all)
          _that.setData({
            "num[0]": all,
          })
          var newData = _that.data.myAgentList1.list;
          newData = res.data.obj.records;
          var newList = {};
          //当长度小于10表示没有下页数据了
          if (listSize < 5) {
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          console.log('newData=' + newData);
          newList.listSize = listSize;
          _that.setData({
            myAgentList1: newList,
            "num1[1]": num2
          });

          console.log('myAgentList1=' + _that.data.myAgentList1);

          _that.setData({
            startNum1: 2
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络通，数据不成功")
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.data)
        // util.msg("错误", "通讯失败")
      }
    })


  },

  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },

  tabClick1: function (e) {
    this.setData({
      sliderOffset1: e.currentTarget.offsetLeft,
      activeIndex1: e.currentTarget.id
    });
  },

  hotActivityLoadMore: function (e) {
    var _that = this;

    var pageNum = _that.data.startNum
    if (_that.data.usertype == 2) {
      var _url1 = api.SalerISList + '?current=' + pageNum + '&size=5';
      var _url2 = api.SalerISList + '?current=' + pageNum + '&size=10';
    } else if (_that.data.usertype == 3) {
      var _url1 = api.SalerOnList + '?current=' + pageNum + '&size=5';
      var _url2 = api.SalerOnList + '?current=' + pageNum + '&size=10';
    }

    if (_that.data.search == false) {

      //判断是否还有下一页
      if (_that.data.myAgentList.hasMore == true) {
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: _url1,
          data: {
            current: pageNum, //第N页
            size: "5"
          },
          method: 'POST',
          header: {
            'Accept': 'application/json',
            'ACCESS-TOKEN': wx.getStorageSync('token')
          },
          success: function (res) {
            console.log(res.data)
            if (res.data.code == "1") {
              console.log("网络通，数据成功")
              wx.hideLoading();
              var listSize = res.data.obj.records.length;
              var newData = _that.data.myAgentList.list;
              //concat是在原有基础上进行增加
              newData = newData.concat(res.data.obj.records);
              var newList = {};
              //当长度等于于0表示当前没有数据，停止
              if (listSize == 0) {
                newList.hasMore = false;
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                return;
                //当长度小于10表示没有下页数据了
              } else if (listSize < 5) {
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                newList.hasMore = false;
              } else {
                newList.hasMore = true;
              }
              newList.list = newData;
              newList.listSize = listSize;
              _that.setData({
                myAgentList: newList
              });

              _that.setData({
                startNum: pageNum + 1 //页数加1
              })
            } else {
              // util.msg("错误", res.data.msg)
              console.log("网络通，数据不成功")
            }

          },
          fail: function (res) {
            console.log(res.data)
            // util.msg("错误", "通讯失败")
            console.log("失败")
          }
        })
      } else {
        wx.showToast({
          title: "已加载全部列表",
          icon: 'none',
          duration: 2000
        });
      }
    } else if (_that.data.search == true) {
      if (_that.data.myAgentList.hasMore == true) {
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: _url2,
          data: {
            deptName: _that.data.inputVal,
          },
          method: 'POST',
          header: {
            'Accept': 'application/json',
            'ACCESS-TOKEN': wx.getStorageSync('token')
          },
          success: function (res) {
            console.log(res.data)
            if (res.data.code == "1") {
              console.log("网络通，数据成功")
              wx.hideLoading();
              var listSize = res.data.obj.records.length;
              var newData = _that.data.myAgentList.list;
              //concat是在原有基础上进行增加
              newData = newData.concat(res.data.obj.records);
              var newList = {};
              //当长度等于于0表示当前没有数据，停止
              if (listSize == 0) {
                newList.hasMore = false;
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                return;
                //当长度小于10表示没有下页数据了
              } else if (listSize < 10) {
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                newList.hasMore = false;
              } else {
                newList.hasMore = true;
              }
              newList.list = newData;
              newList.listSize = listSize;
              _that.setData({
                myAgentList: newList
              });

              _that.setData({
                startNum: pageNum + 1 //页数加1
              })
            } else {
              // util.msg("错误", res.data.msg)
              console.log("网络通，数据不成功")
            }

          },
          fail: function (res) {
            console.log(res.data)
            // util.msg("错误", "通讯失败")
            console.log("失败")
          }
        })
      } else {
        wx.showToast({
          title: "已加载全部列表",
          icon: 'none',
          duration: 2000
        });
      }
    }
  },

  hotActivityLoadMore1: function (e) {
    var _that = this;
    if (_that.data.search == false) {

      //判断是否还有下一页
      if (_that.data.myAgentList1.hasMore == true) {
        wx.showLoading({
          title: '加载中',
        })
        var pageNum = _that.data.startNum1
        var _url = api.SalerCheckList + '?current=' + pageNum + '&size=5';
        wx.request({
          url: _url,
          data: {
            current: pageNum, //第N页
            size: "5"
          },
          method: 'POST',
          header: {
            'Accept': 'application/json',
            'ACCESS-TOKEN': wx.getStorageSync('token')
          },
          success: function (res) {
            console.log(res.data)
            if (res.data.code == "1") {
              console.log("网络通，数据成功")
              wx.hideLoading();
              var listSize = res.data.obj.records.length;
              var newData = _that.data.myAgentList1.list;
              //concat是在原有基础上进行增加
              newData = newData.concat(res.data.obj.records);
              var newList = {};
              //当长度等于于0表示当前没有数据，停止
              if (listSize == 0) {
                newList.hasMore = false;
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                return;
                //当长度小于10表示没有下页数据了
              } else if (listSize < 5) {
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                newList.hasMore = false;
              } else {
                newList.hasMore = true;
              }
              newList.list = newData;
              newList.listSize = listSize;
              _that.setData({
                myAgentList1: newList
              });

              _that.setData({
                startNum1: pageNum + 1 //页数加1
              })
            } else {
              // util.msg("错误", res.data.msg)
              console.log("网络通，数据不成功")
            }

          },
          fail: function (res) {
            console.log(res.data)
            // util.msg("错误", "通讯失败")
            console.log("失败")
          }
        })
      } else {
        wx.showToast({
          title: "已加载全部列表",
          icon: 'none',
          duration: 2000
        });
      }
    } else if (_that.data.search == true) {
      if (_that.data.myAgentList1.hasMore == true) {
        wx.showLoading({
          title: '加载中',
        })
        var pageNum = _that.data.startNum1
        var _url = api.SalerCheckList + '?current=' + pageNum + '&size=10';
        wx.request({
          deptName: _that.data.inputVal,
          url: _url,
          data: {
            current: pageNum, //第N页
            size: "10"
          },
          method: 'POST',
          header: {
            'Accept': 'application/json',
            'ACCESS-TOKEN': wx.getStorageSync('token')
          },
          success: function (res) {
            console.log(res.data)
            if (res.data.code == "1") {
              console.log("网络通，数据成功")
              wx.hideLoading();
              var listSize = res.data.obj.records.length;
              var newData = _that.data.myAgentList1.list;
              //concat是在原有基础上进行增加
              newData = newData.concat(res.data.obj.records);
              var newList = {};
              //当长度等于于0表示当前没有数据，停止
              if (listSize == 0) {
                newList.hasMore = false;
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                return;
                //当长度小于10表示没有下页数据了
              } else if (listSize < 10) {
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                newList.hasMore = false;
              } else {
                newList.hasMore = true;
              }
              newList.list = newData;
              newList.listSize = listSize;
              _that.setData({
                myAgentList1: newList
              });

              _that.setData({
                startNum1: pageNum + 1 //页数加1
              })
            } else {
              // util.msg("错误", res.data.msg)
              console.log("网络通，数据不成功")
            }

          },
          fail: function (res) {
            console.log(res.data)
            // util.msg("错误", "通讯失败")
            console.log("失败")
          }
        })
      } else {
        wx.showToast({
          title: "已加载全部列表",
          icon: 'none',
          duration: 2000
        });
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    var that = this
    this.onLoad()
    console.log('网页刷新')
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})